package com.example.demo.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

@CrossOrigin
@Controller
public class myCon {
    @RequestMapping({"/", "/index"})
    public String toindex(Model model) {
        model.addAttribute("msg", "hello");
        return "/index";
    }

    @RequestMapping({"/user/add"})
    public String add(Model model) {
        model.addAttribute("msg", "add");
        return "/user/add";
    }

    @RequestMapping({"/user/update"})
    public String update(Model model) {
        model.addAttribute("msg", "update");
        return "/user/update";
    }

    @RequestMapping({"/tologin"})
    public String tologin() {
        return "/login";
    }

    @RequestMapping({"/login"})
    public String login(String user, String pwd, Model model) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user, pwd);

        try {
            subject.login(token);
            return "/index";
        } catch (UnknownAccountException e) {

            model.addAttribute("msg", "1");
            return "/login";
        } catch (IncorrectCredentialsException e) {
            model.addAttribute("msg", "2");
            return "/login";
        }

    }

    @RequestMapping({"/noo"})
    public String noo() {
        System.out.println("------------------------");
        return "noo";
    }

    @RequestMapping("/test")
    public String test() {
        return "ok";
    }

}
